package com.zzccyy.jpdemo.page

import android.os.Build
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import com.sztle.admodule.outstatus.AppStatusProcessor
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.base.BasicBaseActivity


class WebActivity : BasicBaseActivity() {
    private var i1: View? = null
    private var web: WebView? = null
    private var titleView: TextView? = null

    override fun isNeedAd(): Boolean {
        return false
    }

    override val layoutId: Int
        get() = R.layout.activity_web

    override fun findViews() {
        i1 = findViewById(R.id.i1)
        web = findViewById(R.id.web)
        titleView = findViewById(R.id.title)
    }

    override fun setListener() {
        i1!!.setOnClickListener { v: View? ->
            finish()
        }
    }

    override fun initData() {
        val intent = intent

        val type = intent?.getIntExtra("type", 0) ?: 0

        // 添加空值检查
        if (web != null) {
            initWebViewAttribute(web!!)

            AppStatusProcessor.statusEnterPrivacyWebPage(web)

            if (type == 0) {
                titleView?.text = "用户协议"
                web!!.loadUrl("http://admin.tgelia.com/xy/user_policy.html?p=blkfjhcpscxzs")
            } else {
                titleView?.text = "隐私政策"
                web!!.loadUrl("http://admin.tgelia.com/xy/privacy_policy.html?p=blkfjhcpscxzs")
            }
        }
    }

    fun initWebViewAttribute(webview: WebView) {
        webview.requestFocus()

        val webSettings = webview.settings
        webSettings.pluginState = WebSettings.PluginState.ON
        webSettings.javaScriptEnabled = true
        webSettings.defaultTextEncodingName = "UTF-8"
        webSettings.cacheMode = WebSettings.LOAD_DEFAULT
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH)
        webSettings.lightTouchEnabled = true
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.domStorageEnabled = true
        webSettings.blockNetworkImage = false
        webSettings.loadWithOverviewMode = true
        if (Build.VERSION.SDK_INT >= 15) {
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.mixedContentMode = 0
        }

        webSettings.allowFileAccess = true
        webSettings.allowContentAccess = true

        webview.webViewClient = WebViewClient()
    }
}